package com.aiit.controller;

import com.aiit.pojo.ALog;
import com.aiit.service.ALogService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("log")
public class ALogController {
    @Resource
    private ALogService alogService;

    //跳转到编辑页面
    @GetMapping("/toEditor")
    public String toEditor() {
        return "editor/editormd";
    }


    //图片上传
    @RequestMapping("/file/upload")
    @ResponseBody
    public JSONObject fileUpload(@RequestParam(value = "editormd-image-file", required = true) MultipartFile file, HttpServletRequest request) throws IOException {
        //上传路径保存设置

        //获得SpringBoot当前项目的路径：System.getProperty("user.dir")
        String path = System.getProperty("user.dir") + "/upload/";

        //按照月份进行分类：
        Calendar instance = Calendar.getInstance();
        String month = (instance.get(Calendar.MONTH) + 1) + "月";
        path = path + month;

        File realPath = new File(path);
        if (!realPath.exists()) {
            realPath.mkdirs();
        }

        //上传文件地址
        System.out.println("上传文件保存地址：" + realPath);

        //解决文件名字问题：我们使用uuid;
        String filename = "pg-" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg";
        File newfile = new File(realPath, filename);
        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(newfile);

        //给editormd进行回调
        JSONObject res = new JSONObject();
        res.put("url", "/upload/" + month + "/" + filename);
        res.put("success", 1);
        res.put("message", "upload success!");

        return res;
    }


    //添加文章
    @PostMapping("/addLog")
    public String addLog(ALog log) {
        alogService.addLog(log);
        return "editor/editormd";
    }

    //查看文章列表
    @GetMapping("logList")
    public String logList(Model model, @RequestParam(value = "pn", defaultValue = "1") int pn, @RequestParam(value = "size", defaultValue = "10") int size) {
        PageHelper.startPage(pn, size);
        List<ALog> logs = alogService.queryAll();
        PageInfo<ALog> page = new PageInfo<>(logs);
        model.addAttribute("page", page);
        return "editor/logs";
    }


    //跳转到具体文章查看页面
    @GetMapping("/{id}")
    public String show(@PathVariable("id") int id, Model model) {
        ALog log = alogService.getLogById(id);
        model.addAttribute("log", log);
        return "editor/log";
    }

    //删除
    @GetMapping("deleteLog")
    public String deleteLog(Integer id) {
        alogService.deleteLogById(id);
        return "editor/logs";
    }
}
